//
//  UIImagePickerController+DF.m
//  微商社区
//
//  Created by nahuo on 15/1/9.
//  Copyright (c) 2015年 nahuo. All rights reserved.
//

#import "UIImagePickerController+DF.h"
#import <objc/runtime.h>
#import "UIActionSheet+DF.h"


@interface UIImagePickerController() <UIImagePickerControllerDelegate,UINavigationControllerDelegate>

typedef void (^CompletionBlock)(NSDictionary *);
typedef void (^CancelBlock)();

@property (nonatomic) CompletionBlock completionBlock;
@property (nonatomic) CancelBlock cancelBlock;

@end


@implementation UIImagePickerController (DF)

-(void)showInViewController:(UIViewController *)viewController completion:(void (^)(NSDictionary *))completion cancel:(void (^)())cancel {
    self.completionBlock = completion;
    self.cancelBlock = cancel;
    
    UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册选择", nil];
    [actionSheet showInView:viewController.view clickedButton:^(UIActionSheet * actionSheet,NSInteger buttonIndex) {
        
        if(buttonIndex==actionSheet.cancelButtonIndex){
            if(self.cancelBlock) {
                self.cancelBlock();
            }
            return;
        }
        
        NSUInteger type = UIImagePickerControllerSourceTypePhotoLibrary;
        if(buttonIndex==0) {
            type = UIImagePickerControllerSourceTypeCamera;
        }
        
        if(![UIImagePickerController isSourceTypeAvailable:type]) {
            return;
        }
        
        // 跳转到相机或相册页面
        
        self.delegate = self;
        self.sourceType = type;
        [viewController presentViewController:self animated:YES completion:nil];
        
    }];
}


#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    if(self.completionBlock) {
        self.completionBlock(info);
    }
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
    if(self.cancelBlock) {
        self.cancelBlock();
    }
}


#pragma mark - 运行时相关
static char completionBlockBlockKey;

-(void)setCompletionBlock:(CompletionBlock)completionBlock {
    [self willChangeValueForKey:@"completionBlock"];
    objc_setAssociatedObject(self, &completionBlockBlockKey,
                             completionBlock,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"completionBlock"];
}

-(CompletionBlock)completionBlock {
    return objc_getAssociatedObject(self, &completionBlockBlockKey);
}

static char cancelBlockBlockKey;

-(void)setCancelBlock:(CancelBlock)cancelBlock {
    [self willChangeValueForKey:@"cancelBlock"];
    objc_setAssociatedObject(self, &cancelBlockBlockKey,
                             cancelBlock,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"cancelBlock"];
}

-(CancelBlock)cancelBlock {
    return objc_getAssociatedObject(self, &cancelBlockBlockKey);
}

@end
